package borge.web.teamspirit.client.page.team.widgets;

import java.util.List;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.TeamSpiritConstants;
import borge.web.teamspirit.client.data.Match;
import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.ServerCallbackHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.PersistentData.DataKey;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;
import borge.web.teamspirit.client.page.shared.widgets.ListDataWidget;
import borge.web.teamspirit.client.page.team.TokenData;

@SuppressWarnings("unchecked")
public class ListMatchesWidget extends ListDataWidget<PersistentData> {

    //private List<Match> iMatches;
    
    public ListMatchesWidget(WidgetEventHandler aEventHandler, List<PersistentData> aMatches){
        super(aEventHandler, aMatches, DataKey.MATCH, TokenData.assembleTeamToken(TokenData.EDIT, TokenData.MATCH), 
                WidgetEventType.DELETE_MATCH,
                TokenData.assembleTeamToken(TokenData.SHOW, TokenData.MATCH));
        
        //iData = aMatches;
        if(iData != null)
        	createDataTable();
        
        initWidget(iPanel);
    }
    
    protected void createDataTable(){
        

        reset();

        TeamSpiritConstants lcs = TeamSpirit.getConstants();
        int row = 0;
        int col = 0;

        // We should only show first name, second name, edit and delete later
        
        iTable.setText(row, col++, lcs.id());

        iTable.setText(row, col++, lcs.opponents());
        //iTable.setText(row, col++, lcs);

        //iTable.setText(row, col++, lcs.edit());
        //iTable.setText(row, col++, lcs.delete());
        row++;

        int index = 0;
        for (PersistentData pd : iData) {
            col = 0;
            Match m = (Match)pd;

            //iTable.setText(row, col++, m.getMatchId().toString());
            iTable.setText(row,col++, m.getEncodedKey());
            iTable.setText(row, col++, m.getOpponents());
            addButtons(row, col, Integer.toString(index++));
            row++;
        }

        //iPanel.add(iTable);
        
    }
    
	@Override
	public void requestServerData(ServerCallbackHandler aHandler) {
		
	}
}
